<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="前言mongo如何根据每条记录的时间分组查询数据？比如查询用户每一天上传了哪些图片，最终返回结果是时间作为key值，value值是一个多张图片的数组。简单的想的话可以先查询到用户上传的所有图片，然后前端再做数组便利，将这些图片分组。那再数据量很大的时候又该怎么处理呢？mongodb有提供一个aggregate方法，就是用来应对这种分组查询的。 数据准备假设现在有一张名叫histories的表，它的">
<meta name="keywords" content="mongodb">
<meta property="og:type" content="article">
<meta property="og:title" content="mongodb聚合查询">
<meta property="og:url" content="https://andyliwr.github.io/2017/11/06/mongodb_aggregate/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="前言mongo如何根据每条记录的时间分组查询数据？比如查询用户每一天上传了哪些图片，最终返回结果是时间作为key值，value值是一个多张图片的数组。简单的想的话可以先查询到用户上传的所有图片，然后前端再做数组便利，将这些图片分组。那再数据量很大的时候又该怎么处理呢？mongodb有提供一个aggregate方法，就是用来应对这种分组查询的。 数据准备假设现在有一张名叫histories的表，它的">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-01-15T06:30:09.116Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mongodb聚合查询">
<meta name="twitter:description" content="前言mongo如何根据每条记录的时间分组查询数据？比如查询用户每一天上传了哪些图片，最终返回结果是时间作为key值，value值是一个多张图片的数组。简单的想的话可以先查询到用户上传的所有图片，然后前端再做数组便利，将这些图片分组。那再数据量很大的时候又该怎么处理呢？mongodb有提供一个aggregate方法，就是用来应对这种分组查询的。 数据准备假设现在有一张名叫histories的表，它的">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>mongodb聚合查询</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2017/11/08/nodejs_identicon/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2017/11/03/node_env_usage/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&text=mongodb聚合查询"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&is_video=false&description=mongodb聚合查询"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=mongodb聚合查询&body=Check out this article: https://andyliwr.github.io/2017/11/06/mongodb_aggregate/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&name=mongodb聚合查询&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数据准备"><span class="toc-number">2.</span> <span class="toc-text">数据准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#学习-aggregate"><span class="toc-number">3.</span> <span class="toc-text">学习 aggregate</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        mongodb聚合查询
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2017-11-06T14:37:00.000Z" itemprop="datePublished">2017-11-06</time>
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/mongodb/">mongodb</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h4><p><code>mongo</code>如何根据每条记录的时间分组查询数据？比如查询用户每一天上传了哪些图片，最终返回结果是时间作为<code>key</code>值，<code>value</code>值是一个多张图片的数组。简单的想的话可以先查询到用户上传的所有图片，然后前端再做数组便利，将这些图片分组。那再数据量很大的时候又该怎么处理呢？<br><code>mongodb</code>有提供一个<code>aggregate</code>方法，就是用来应对这种分组查询的。</p>
<h4 id="数据准备"><a href="#数据准备" class="headerlink" title="数据准备"></a>数据准备</h4><p>假设现在有一张名叫<code>histories</code>的表，它的数据结构如下，点击<a href="http://ouizhbgin.bkt.clouddn.com/blog/2017/11/06/mongo_history_data.json" target="_blank" rel="noopener">这里</a>下载全部数据：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"_id"</span>: ObjectId(<span class="string">"59ffb36d662314030cdad763"</span>),</span><br><span class="line">  <span class="attr">"filename"</span>: <span class="string">"1509929837131.png"</span>,</span><br><span class="line">  <span class="attr">"old_filename"</span>: <span class="string">"mysql_system_path.PNG"</span>,</span><br><span class="line">  <span class="attr">"filesize"</span>: <span class="string">"1509929837.126"</span>,</span><br><span class="line">  <span class="attr">"userid"</span>: ObjectId(<span class="string">"59f7d33009bcb137983415f6"</span>),</span><br><span class="line">  <span class="attr">"tmp_url"</span>: <span class="string">"D:\\PROJECT\\ldk-upload-img\\uploads\\upload_1053904e8f5135f8641505a9f5c5ef72.PNG"</span>,</span><br><span class="line">  <span class="attr">"remote_url"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509929837131.png"</span>,</span><br><span class="line">  <span class="attr">"time"</span>: ISODate(<span class="string">"2017-11-05T00:57:17.228Z"</span>),</span><br><span class="line">  <span class="attr">"__v"</span>: <span class="number">0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们要做的就是查找出<code>useri</code>d 为<code>“59f7d33009bcb137983415f6”</code>的用户每一天上传了那些图片，也就是<code>old_filename</code>和<code>remote_url</code>这两个字段。</p>
<h4 id="学习-aggregate"><a href="#学习-aggregate" class="headerlink" title="学习 aggregate"></a>学习 aggregate</h4><p>先来学习聚合常用的操作：</p>
<ul>
<li><code>$project</code>：修改输入文档的结构。可以用来重命名、增加或删除域，也可以用于创建计算结果以及嵌套文档。</li>
<li><code>$match</code>：用于过滤数据，只输出符合条件的文档。<code>$match</code>使用<code>MongoDB</code>的标准查询操作。</li>
<li><code>$limit</code>：用来限制<code>MongoDB</code>聚合管道返回的文档数。</li>
<li><code>$skip</code>：在聚合管道中跳过指定数量的文档，并返回余下的文档。</li>
<li><code>$unwind</code>：将文档中的某一个数组类型字段拆分成多条，每条包含数组中的一个值。</li>
<li><code>$group</code>：将集合中的文档分组，可用于统计结果。</li>
<li><code>$sort</code>：将输入文档排序后输出。</li>
<li><code>$geoNear</code>：输出接近某一地理位置的有序文档</li>
</ul>
<p>然后浏览下聚合一些常用的表达式</p>
<ul>
<li><code>$sum</code> 计算总和统计每个用户上传图片数量总数</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.histories.aggregate([&#123;$group : &#123;_id : &quot;$userid&quot;, upload_num : &#123;$sum : 1&#125;&#125;&#125;])</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f8600b06445e4780bb0a69&quot;), &quot;upload_num&quot; : 6 &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f7d33009bcb137983415f6&quot;), &quot;upload_num&quot; : 2 &#125;</span><br></pre></td></tr></table></figure>
<p>统计每个用户上传图片的总大小</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.histories.aggregate([&#123;$group : &#123;_id : &quot;$userid&quot;, upload_num : &#123;$sum : &quot;$filesize&quot;&#125;&#125;&#125;])</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f8600b06445e4780bb0a69&quot;), &quot;upload_num&quot; : 10569636779.855999 &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f7d33009bcb137983415f6&quot;), &quot;upload_num&quot; : 3019860521.908 &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>$avg</code> 计算平均值统计每个用户上传图片平均大小，这个有些无聊&gt;_&lt;</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.histories.aggregate([&#123;$group : &#123;_id : &quot;$userid&quot;, upload_num : &#123;$avg : &quot;$filesize&quot;&#125;&#125;&#125;])</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f8600b06445e4780bb0a69&quot;), &quot;upload_num&quot; : 1509948111.4079998 &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f7d33009bcb137983415f6&quot;), &quot;upload_num&quot; : 1509930260.954 &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>$max</code>和<code>$min</code> 取得最大值和最小值计算得到用户所有上传历史里文件大小最大和最小的数值</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.histories.aggregate([&#123;$group : &#123;_id : &quot;$userid&quot;, upload_max : &#123;$max : &quot;$filesize&quot;&#125;, upload_min : &#123;$min : &quot;$filesize&quot;&#125;&#125;&#125;])</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f8600b06445e4780bb0a69&quot;), &quot;upload_max&quot; : 1509951997.92, &quot;upload_min&quot; : 1509947349.678 &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;59f7d33009bcb137983415f6&quot;), &quot;upload_max&quot; : 1509930684.782, &quot;upload_min&quot; : 1509929837.126 &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>$push</code> 很重要的一个计算表达式，比如我要统计用户每天内上传了那些图片</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.histories.aggregate([</span><br><span class="line">   &#123;</span><br><span class="line">     $project: &#123;</span><br><span class="line">       day: &#123;</span><br><span class="line">           $substr: [&#123; &quot;$add&quot;: [&quot;$time&quot;, 28800000] &#125;, 0, 10]</span><br><span class="line">       &#125;,</span><br><span class="line">       &quot;old_filename&quot;: 1,</span><br><span class="line">       &quot;remote_url&quot;: 1</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;,</span><br><span class="line">   &#123;</span><br><span class="line">     $group: &#123;</span><br><span class="line">       _id: &quot;$day&quot;,</span><br><span class="line">       &quot;upload_arr&quot;: &#123; $push: &#123; name: &quot;$old_filename&quot;, link: &quot;$remote_url&quot; &#125; &#125;</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;</span><br><span class="line"> ])</span><br></pre></td></tr></table></figure>
<p><code>$project</code>就是定义会有那些字段参与到统计，中途可以创建全新的字段，或者对字段做格式化，比如为了拿到某一天的日期，我们需要将原本以 UTC 格式的存储的时间字段$time 加上<code>8*60*60*1000</code>毫秒就是 8 天的时间，这样时间就变成了北京时间。然后使用<code>$substr</code>操作截取字段的前 11 位，比如<code>&quot;2017-11-06 15:28:00&quot;</code>就变成了<code>&quot;2017-11-06&quot;</code>。另外由于在最终的统计结果中用到了文件名<code>old_filename</code>，以及远程地址<code>remote_url</code>，所以我们需要在<code>$project</code>中将它的值设置为 1，表示它需要被保留。在$group 中就可以对保留下来的数据做分组处理，_id 是定义根据什么来分组，这里显然是根据时间，也就是新生成的 day 字段，最后新建一个 upload_arr 字段，用来保存所有在这一天内的上传的图片的信息。这下你明白<code>$push</code>的作用了吧，就是在结果文档中插入值到一个数组中。结果如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"_id"</span>: <span class="string">"2017-11-06"</span>,</span><br><span class="line">  <span class="attr">"upload_arr"</span>: [</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"mysql_system_path.PNG"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509930684790.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"github.PNG"</span>,<span class="attr">"link"</span>: <span class="string">"http: //oyh0gj8ht.bkt.clouddn.com/1509947349681.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"mysql_start.png"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509947447592.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"mysql_system_path.PNG"</span>,<span class="attr">"link"</span>: <span class="string">"http: //oyh0gj8ht.bkt.clouddn.com/1509947455689.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"Capture.jpg"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509947466105.jpg"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"andyliwr-ftp.png"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509947471882.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"post20170912_01.png"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509947591007.png"</span>&#125;,</span><br><span class="line">      &#123;<span class="attr">"name"</span>: <span class="string">"check.png"</span>,<span class="attr">"link"</span>: <span class="string">"http: //oyh0gj8ht.bkt.clouddn.com/1509951997921.png"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;,</span><br><span class="line">&#123;</span><br><span class="line">  <span class="attr">"_id"</span>: <span class="string">"2017-11-05"</span>,</span><br><span class="line">  <span class="attr">"upload_arr"</span>: [&#123;<span class="attr">"name"</span>: <span class="string">"mysql_system_path.PNG"</span>,<span class="attr">"link"</span>: <span class="string">"http://oyh0gj8ht.bkt.clouddn.com/1509929837131.png"</span>&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>其他一些表达式：</li>
</ul>
<ol>
<li><code>$addToSet</code>, 在结果文档中插入值到一个数组中，但不创建副本。</li>
<li><code>$first</code>，根据资源文档的排序获取第一个文档数据。</li>
<li><code>$last</code>，根据资源文档的排序获取最后一个文档数据。</li>
</ol>
<p>哎，当时没有时间去研究聚合查询，否则她说的那个<code>mongo</code>查询一堆数据最早时间和最晚时间解决起来不是分分钟的事情？我知道自己一直不够优秀，所以才会想要努力变得强大，你会相信我可以做到吗?</p>
<p>最后贴下我的最终代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">.aggregate([</span><br><span class="line">  &#123;</span><br><span class="line">    $match: &#123;</span><br><span class="line">        &quot;userid&quot;: ObjectId(&quot;59f7d33009bcb137983415f6&quot;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    $project: &#123;</span><br><span class="line">      day: &#123;</span><br><span class="line">          $substr: [&#123; &quot;$add&quot;: [&quot;$time&quot;, 28800000] &#125;, 0, 10]</span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;old_filename&quot;: 1,</span><br><span class="line">      &quot;remote_url&quot;: 1</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    $group: &#123;</span><br><span class="line">      _id: &quot;$day&quot;,</span><br><span class="line">      &quot;upload_arr&quot;: &#123; $push: &#123; name: &quot;$old_filename&quot;, link: &quot;$remote_url&quot; &#125; &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">])</span><br></pre></td></tr></table></figure>

  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数据准备"><span class="toc-number">2.</span> <span class="toc-text">数据准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#学习-aggregate"><span class="toc-number">3.</span> <span class="toc-text">学习 aggregate</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&text=mongodb聚合查询"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&is_video=false&description=mongodb聚合查询"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=mongodb聚合查询&body=Check out this article: https://andyliwr.github.io/2017/11/06/mongodb_aggregate/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&title=mongodb聚合查询"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/11/06/mongodb_aggregate/&name=mongodb聚合查询&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


